﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FATCATool
{
    public partial class AddressControl : UserControl
    {
        private CorrectableOrganisationParty_Type _correctableParty;
        private OrganisationParty_Type _party;
        private PersonParty_Type _person;
        private bool _bindingComplete = false;
        private FATCASchema.Wrappers.Addresses _addresses;

        public AddressControl()
        {
            InitializeComponent();
        }

        public void Initialize(PersonParty_Type party)
        {
            _bindingComplete = false;
            _person = party;
            _addresses = FATCASchema.Wrappers.Addresses.GetWrapperAddresses(_person.Address);
            DataBind();
            _bindingComplete = true;
        }

        public void Initialize(CorrectableOrganisationParty_Type party)
        {
            _bindingComplete = false;
            _correctableParty = party;
            _addresses = FATCASchema.Wrappers.Addresses.GetWrapperAddresses(_correctableParty.Address);
            DataBind();
            _bindingComplete = true;
        }

        public void Initialize(OrganisationParty_Type party)
        {
            _bindingComplete = false;
            _party = party;
            _addresses = FATCASchema.Wrappers.Addresses.GetWrapperAddresses(_party.Address);
            DataBind();
            _bindingComplete = true;
        }

        private void DataBind()
        {
            ((DataGridViewComboBoxColumn)grdAddresses.Columns["addressTypeDataGridViewTextBoxColumn"]).DataSource = FATCA_OECD.GetFriendlyEnumList(typeof(OECDLegalAddressType_EnumType));
            ((DataGridViewComboBoxColumn)grdAddresses.Columns["addressTypeDataGridViewTextBoxColumn"]).ValueMember = "Key";
            ((DataGridViewComboBoxColumn)grdAddresses.Columns["addressTypeDataGridViewTextBoxColumn"]).DisplayMember = "Value";

            addressBindingSource.DataSource = null;
            addressBindingSource.DataSource = _addresses;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var address = new FATCASchema.Wrappers.Address();

            //show object in details screen
            var cntl = new AddressDetailControl();
            cntl.Address = address;
            Form form = new Form
            {
                Text = "Address Edit",
                Size = new Size(465, 530)
            };
            form.Controls.Add(cntl);

            if (form.ShowDialog() == DialogResult.OK)
            {
                _addresses.Add(cntl.Address);
                if (_correctableParty != null)
                    _correctableParty.Address = _addresses.ToFATCAArray();
                else if (_party != null)
                    _party.Address = _addresses.ToFATCAArray();
                else
                    _person.Address = _addresses.ToFATCAArray();

                DataBind();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (grdAddresses.SelectedRows.Count != 1 || !(grdAddresses.SelectedRows[0].DataBoundItem is FATCASchema.Wrappers.Address))
                return;

            var address = (FATCASchema.Wrappers.Address)grdAddresses.SelectedRows[0].DataBoundItem;


            var cntl = new AddressDetailControl();
            cntl.Address = address;
            Form form = new Form
            {
                Text = "Address Edit",
                Size = new Size(465, 530)
            };
            form.Controls.Add(cntl);

            if (form.ShowDialog() == DialogResult.OK)
            {
                _addresses[_addresses.IndexOf(cntl.Address)] = cntl.Address;
                if (_correctableParty != null)
                    _correctableParty.Address = _addresses.ToFATCAArray();
                else if (_party != null)
                    _party.Address = _addresses.ToFATCAArray();
                else
                    _person.Address = _addresses.ToFATCAArray();

                DataBind();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (grdAddresses.SelectedRows.Count != 1 || !(grdAddresses.SelectedRows[0].DataBoundItem is FATCASchema.Wrappers.Address))
                return;

            var address = (FATCASchema.Wrappers.Address)grdAddresses.SelectedRows[0].DataBoundItem;

            _addresses.Remove(address);
            if (_correctableParty != null)
                _correctableParty.Address = _addresses.ToFATCAArray();
            else if (_party != null)
                _party.Address = _addresses.ToFATCAArray();
            else
                _person.Address = _addresses.ToFATCAArray();

            DataBind();
        }
    }
}
